package Dao.Role.Imp;

import Dao.Role.AllTeacherDao;
import Entity.Role.Teacher;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AllTeacherDaoImp implements AllTeacherDao {

    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public List<Teacher> getAllTea(Connection conn) {
        List<Teacher> teachers = new ArrayList<>();
        try {
            String sql = "select tno,password,name,sex,post,tel,email,collegeid,department,role ,email_code " +
                    "from teacher";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                String NO = rs.getString("tno");
                String password = rs.getString("password");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                String post = rs.getString("post");
                String tel = rs.getString("tel");
                String email = rs.getString("email");
                String collId = rs.getString("collegeid");
                String department = rs.getString("department");
                String role = rs.getString("role");
                String emailCode = rs.getString("email_code");
                Teacher teacher = new Teacher(NO, password, name, sex, post, tel, email, collId, department, role,emailCode);
                teachers.add(teacher);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return teachers;
    }
}
